package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.entity.MqSchedulingLog;
import com.xmy.cultivate.entity.MultiDelayMessage;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.mapper.MqSchedulingLogMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 上课提醒与扣课时 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-06-12
 */
@Service
public class MqSchedulingLogServiceImpl extends ServiceImpl<MqSchedulingLogMapper, MqSchedulingLog> implements IMqSchedulingLogService {

    @Autowired
    MqSchedulingLogMapper mqSchedulingLogMapper;

    @Autowired
    ICourseSchedulingDetailService iCourseSchedulingDetailService;

    @Autowired
    IRepairCourseService iRepairCourseService;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IMqPushService iMqPushService;

    @Async("onlineAsync")
    @Override
    public void addData(Integer yearPart, Integer quarterNum, String schoolDate, String startDateTime, String endDateTime, String lessonType){
        if (yearPart == null){
            Quarter quarterNow = iQuarterService.getNowQuarter();
            if (quarterNow != null){
                yearPart = quarterNow.getYearPart();
                quarterNum = quarterNow.getNum();
            }else {
                yearPart = 0;
                quarterNum = 0;
            }
        }

        List<MqSchedulingLog> mqSchedulingLogList = mqSchedulingLogMapper.getSchedulingList(yearPart,quarterNum,schoolDate,startDateTime,endDateTime,lessonType);
        for (MqSchedulingLog mqSchedulingLog :mqSchedulingLogList){
            mqSchedulingLog.setType(2);
            this.addSchedulingLog(mqSchedulingLog);
        }
    }
    public boolean addSchedulingLog(MqSchedulingLog mqSchedulingLog){
        try {
            LocalDateTime dateTimeNow = LocalDateTime.now().withNano(0);
            Duration duration = Duration.between(dateTimeNow,mqSchedulingLog.getStartDateTime());
            long minutsDiff = duration.getSeconds();//距离现在相差多少秒
            //大于5秒以上的
            if (minutsDiff > 0){
                Long delayMinuts = null;//多少秒后
                Long towHouseMinuts = 7200L;
                Long halfHouseMinuts = 1800L;
                Long[] moreDelay = {};
                ArrayList<Long> moreDelayList = new ArrayList<>();
                delayMinuts = minutsDiff - towHouseMinuts;
                if (delayMinuts >= 0){
                    Long diffMinuts = towHouseMinuts - halfHouseMinuts;
                    moreDelayList.add(diffMinuts);//添加两小时后的1个3小时，及上课前30分钟
                }else {
                    delayMinuts = minutsDiff - halfHouseMinuts;//半小时
                }
                Integer moreSize = moreDelayList.size();
                if (moreSize>0){
                    moreDelay = new Long[moreSize];
                    for (Integer i=0;i<moreSize;i++){
                        moreDelay[i] = moreDelayList.get(i);
                    }
                }

                MqSchedulingLog mqSchedulingLogInfo = this.getById(mqSchedulingLog.getId());
                if (mqSchedulingLogInfo == null){
                    mqSchedulingLogInfo = new MqSchedulingLog();
                    BeanUtils.copyProperties(mqSchedulingLog,mqSchedulingLogInfo);
                    mqSchedulingLogInfo.setStatus(0);
                    mqSchedulingLogInfo.setDelayedSecond(delayMinuts);
                    mqSchedulingLogInfo.setExecuteTime(dateTimeNow.plusSeconds(delayMinuts));
                    this.save(mqSchedulingLogInfo);
                    if (delayMinuts != null){
                        MultiDelayMessage<MqSchedulingLog> multiDelayMessage = MultiDelayMessage.of(mqSchedulingLogInfo,moreDelay);
                        iMqPushService.classUpRemindMulti(multiDelayMessage,Integer.parseInt(delayMinuts.toString()));
                    }

                    mqSchedulingLogInfo.setStatus(1);
                    this.updateById(mqSchedulingLogInfo);
                }else {
                    mqSchedulingLogInfo.setSchoolDate(mqSchedulingLog.getSchoolDate());
                    mqSchedulingLogInfo.setStartDateTime(mqSchedulingLog.getStartDateTime());
                    mqSchedulingLogInfo.setEndDateTime(mqSchedulingLog.getEndDateTime());
                    mqSchedulingLogInfo.setDelayedSecond(delayMinuts);
                    mqSchedulingLogInfo.setExecuteTime(dateTimeNow.plusSeconds(delayMinuts));

                    this.updateById(mqSchedulingLogInfo);
                    if (mqSchedulingLog.getStartDateTime() != mqSchedulingLogInfo.getStartDateTime() || mqSchedulingLog.getStatus().equals(0)){
                        if (delayMinuts >=0){
                            MultiDelayMessage<MqSchedulingLog> multiDelayMessage = MultiDelayMessage.of(mqSchedulingLogInfo,moreDelay);
                            iMqPushService.classUpRemindMulti(multiDelayMessage,Integer.parseInt(delayMinuts.toString()));
                        }
                        mqSchedulingLogInfo.setStatus(1);
                        this.updateById(mqSchedulingLogInfo);
                    }
                }
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return true;
    }
}
